﻿using SqlSugar;

namespace Koala.Pro.CodeGenerator.UI.Common
{
    [SugarTable("TableNameAlias")]
    public class TableNameAliasInfo
    {
        [SugarColumn(IsPrimaryKey = true, IsIdentity = true)]
        public int ID { get; set; }

        public string TableName { get; set; }

        public string TableAlias { get; set; }

        public string DatabaseType { get; set; }

        public static TableNameAliasInfo GetAliasByName(string tableName, string databaseType)
        {
            var db = CodeGenerationSettingDbContext.GetDbClient();
            return db.Queryable<TableNameAliasInfo>().Single(x => x.TableName == tableName && x.DatabaseType == databaseType);
        }

        public void InsertOrUpdate()
        {
            var db = CodeGenerationSettingDbContext.GetDbClient();
            string empty = string.Empty;
            TableNameAliasInfo info = GetAliasByName(TableName, DatabaseType);
            if (info == null)
            {
                db.Insertable(this).ExecuteCommand();
            }
            else
            {
                db.Updateable(this).ExecuteCommand();
            }
        }
    }
}
